package qiutan

import (
	"whoscored/util/boltdb"
	"strconv"
	"strings"
	"regexp"
	"math"
	"encoding/json"
)

func checkAsia(data string) (bool,[]float64) {
	asiaSlice:=strings.Split(data,"!")

	//初盘均值
	s1:=float64(0)
	//终盘均值
	s2:=float64(0)
	p1:=float64(0)
	p2:=float64(0)
	p3:=float64(0)
	p4:=float64(0)
	for _,v:=range asiaSlice{
		asiaOdds:=strings.Split(v,"^")
		if (len(asiaOdds)<7){
			continue
		}
		name:=asiaOdds[0]
		if name=="澳门"{
			p1,_=strconv.ParseFloat(asiaOdds[2],64)
			s1,_=strconv.ParseFloat(asiaOdds[3],64)
			p2,_=strconv.ParseFloat(asiaOdds[4],64)
			p3,_=strconv.ParseFloat(asiaOdds[5],64)
			s2,_=strconv.ParseFloat(asiaOdds[6],64)
			p4,_=strconv.ParseFloat(asiaOdds[7],64)
			break
		}


	}
	if p1==0{
		return false,nil
	}

	return true,[]float64{p1,s1,p2,p3,s2,p4}
}
func leagueData(text string) (bool,[]float64) {

	s:=strings.Split(text,"$$$$")

	ll:=strings.Split(s[0],"$$")

	if len(ll)<2||!strings.Contains(ll[0],"^")||!strings.Contains(ll[1],"^"){
		return false,nil
	}

	home:=strings.Split(ll[0],"!")
	away:=strings.Split(ll[1],"!")

	//主队数据
	homeAllSlice:=strings.Split(home[0],"^")

	homeAllWin,_:=strconv.ParseFloat(homeAllSlice[2],64)
	homeAllDraw,_:=strconv.ParseFloat(homeAllSlice[3],64)
	homeAllLoss,_:=strconv.ParseFloat(homeAllSlice[4],64)
	if (homeAllWin+homeAllDraw+homeAllLoss)==0{
		return false,nil
	}
	//主队胜率
	homeAllWinPercent:=homeAllWin/(homeAllWin+homeAllDraw+homeAllLoss)
	//主队不败率
	homeAllNotLossPercent:=(homeAllWin+homeAllDraw)/(homeAllWin+homeAllDraw+homeAllLoss)

	//主队主场数据
	homeHomeSlice:=strings.Split(home[1],"^")

	homeHomeWin,_:=strconv.ParseFloat(homeHomeSlice[2],64)
	homeHomeDraw,_:=strconv.ParseFloat(homeHomeSlice[3],64)
	homeHomeLoss,_:=strconv.ParseFloat(homeHomeSlice[4],64)

	if (homeHomeWin+homeHomeDraw+homeHomeLoss)==0{
		return false,nil
	}
	//主队主场胜率
	homeHomeWinPercent:=homeHomeWin/(homeHomeWin+homeHomeDraw+homeHomeLoss)
	//主队主场不败率
	homeHomeNotLossPercent:=(homeHomeWin+homeHomeDraw)/(homeHomeWin+homeHomeDraw+homeHomeLoss)

	//客场数据
	awayAllSlice:=strings.Split(away[0],"^")

	awayAllWin,_:=strconv.ParseFloat(awayAllSlice[2],64)
	awayAllDraw,_:=strconv.ParseFloat(awayAllSlice[3],64)
	awayAllLoss,_:=strconv.ParseFloat(awayAllSlice[4],64)
	if (awayAllWin+awayAllDraw+awayAllLoss)==0{
		return false,nil
	}
	//客队胜率
	awayAllWinPercent:=awayAllWin/(awayAllWin+awayAllDraw+awayAllLoss)
	//客队不败率
	awayAllNotLossPercent:=(awayAllWin+awayAllDraw)/(awayAllWin+awayAllDraw+awayAllLoss)

	//客队客场数据
	awayAwaySlice:=strings.Split(away[2],"^")

	awayAwayWin,_:=strconv.ParseFloat(awayAwaySlice[2],64)
	awayAwayDraw,_:=strconv.ParseFloat(awayAwaySlice[3],64)
	awayAwayLoss,_:=strconv.ParseFloat(awayAwaySlice[4],64)
	if (awayAwayWin+awayAwayDraw+awayAwayLoss)==0{
		return false,nil
	}
	//客队客场胜率
	awayAwayWinPercent:=awayAwayWin/(awayAwayWin+awayAwayDraw+awayAwayLoss)
	//客队客场不败率
	awayAwayNotLossPercent:=(awayAwayWin+awayAwayDraw)/(awayAwayWin+awayAwayDraw+awayAwayLoss)

	//主队得分能力
	homeAllWinScore,_:=strconv.ParseFloat(homeAllSlice[5],64)
	homeAllLossScore,_:=strconv.ParseFloat(homeAllSlice[6],64)
	homeAllScore:=homeAllWinScore/(homeAllWinScore+homeAllLossScore)
	//主队主场得分能力
	homeHomeWinScore,_:=strconv.ParseFloat(homeHomeSlice[5],64)
	homeHomeLossScore,_:=strconv.ParseFloat(homeHomeSlice[6],64)
	homeHomeScore:=homeHomeWinScore/(homeHomeWinScore+homeHomeLossScore)

	//客队得分能力
	awayAllWinScore,_:=strconv.ParseFloat(awayAllSlice[5],64)
	awayAllLossScore,_:=strconv.ParseFloat(awayAllSlice[6],64)
	awayAllScore:=awayAllWinScore/(awayAllWinScore+awayAllLossScore)
	//客队客场得分能力
	awayAwayWinScore,_:=strconv.ParseFloat(awayAwaySlice[5],64)
	awayAwayLossScore,_:=strconv.ParseFloat(awayAwaySlice[6],64)
	awayAwayScore:=awayAwayWinScore/(awayAwayWinScore+awayAwayLossScore)

	//联赛排名
	homeRank,_:=strconv.ParseFloat(homeAllSlice[8],64)
	awayRank,_:=strconv.ParseFloat(awayAllSlice[8],64)

	return true,[]float64{
		homeAllWinPercent-awayAllWinPercent,
		homeAllNotLossPercent-awayAllNotLossPercent,
		homeHomeWinPercent,awayAwayWinPercent,
		homeHomeNotLossPercent,awayAwayNotLossPercent,
		homeAllScore-awayAllScore,
		homeHomeScore-awayAwayScore,
		homeRank-awayRank,
	}
}
/**
交战数据
[]f1 [胜率，平率，负率]
[]f2 [交锋进球数占比差]
 */
func betData(startAt string,t1 string,t2 string,text string) (bool,[]float64) {
	reg, _ := regexp.Compile("\\[[\\S\\s]+?]")
	//主队
	t1= reg.ReplaceAllString(t1, "")
	//客队
	t2= reg.ReplaceAllString(t2, "")
	s:=strings.Split(text,"$$$$")

	m:=strings.Split(s[1],"$$")
	//最近交锋数据处理
	bet:=strings.Split(m[0],"!")
	betWin:=float64(0)
	betFit:=float64(0)
	betLoss:=float64(0)
	scoreTotal:=float64(0)
	scoreHome:=float64(0)
	scoreGuest:=float64(0)
	betTotal:=float64(0)

	//赢盘
	wpk:=float64(0)
	dpk:=float64(0)
	lpk:=float64(0)
	//均盘口
	pkTotal:=float64(0)

	for _,v:=range bet{
		slice:=strings.Split(v,"^")

		if v==""||len(slice)<11||slice[10]==""{
			continue
		}
		k:=slice[10]
		if k=="W"{
			wpk++
		}
		if k=="D"{
			dpk++
		}
		if k=="L"{
			lpk++
		}
		pk,_:=strconv.ParseFloat(slice[11],64)
		pk=math.Abs(pk)
		pkTotal+=pk
		/*
		//比赛时间不超过一年
		time1,_:=time.Parse("20060102150405",slice[0])
		time2,_:=time.Parse("06-01-02 15:04",startAt)

		if time2.Sub(time1).Hours()>24*365{
			continue
		}
		if time2.Year()-time1.Year()>1{
			continue
		}*/

		ta:=slice[2]
		sa,_:=strconv.ParseFloat(slice[4],64)
		sb,_:=strconv.ParseFloat(slice[5],64)

		if strings.Index(ta,t1)>=0{

			scoreTotal+=sa+sb
			scoreHome+=sa
			scoreGuest+=sb
			if sa>sb{
				betWin++
			}
			if sa==sb{
				betFit++
			}
			if sa<sb{
				betLoss++
			}
		}else{
			scoreTotal+=sa+sb
			scoreHome+=sb
			scoreGuest+=sa
			if sa<sb{
				betWin++
			}
			if sa==sb{
				betFit++
			}
			if sa>sb{
				betLoss++
			}
		}
		betTotal++
	}

	//交锋胜率
	f1:=float64(0)
	//交锋平率
	f2:=float64(0)
	//交锋负率
	f3:=float64(0)
	//交锋进球数占比差
	f4:=float64(0)
	pkAvg:=pkTotal/betTotal
	//赢盘率
	f5:=wpk/(wpk+dpk+lpk)
	f6:=(wpk+dpk)/(wpk+dpk+lpk)
	if betTotal>0{
		f1=betWin/betTotal
		f2=betFit/betTotal
		f3=(betTotal-betWin-betFit)/betTotal
		if scoreTotal>0{
			f4=(scoreHome-scoreGuest)/scoreTotal
		}

		return true,[]float64{f1,f2,f3,f4,pkAvg,f5,f6}
	}
	return false,nil
}
func europeData(data string) (bool,[]float64) {
	slice := make([]string, 0)
	json.Unmarshal([]byte(data), &slice)
	f1:=float64(0)
	//f2:=float64(0)
	//f3:=float64(0)
	f4:=float64(0)
	f5:=float64(0)
	f6:=float64(0)

	for _, v := range slice {
		d := strings.Split(v, "|")

		size:=len(d)
		if d[size-3]=="SB"{
			f4, _ = strconv.ParseFloat(d[3], 64)
			f5, _ = strconv.ParseFloat(d[4], 64)
			f6, _ = strconv.ParseFloat(d[5], 64)
			f1, _ = strconv.ParseFloat(d[10], 64)
			//f2, _ = strconv.ParseFloat(d[11], 64)
			//f3, _ = strconv.ParseFloat(d[12], 64)

		}


	}
	if f1==0{
		return false,nil
	}

	return true,[]float64{f4,f5,f6,}
}

func Format(startAt string,matchId int64,league string,home string,away string,fn func([]float64,[]float64,[]float64,[]float64,[]float64))  {
	k:=strconv.FormatInt(matchId,10)
	year:=strings.Split(startAt,"-")[0]
	boltdb.Get("asia-"+year,league,k, func(asia string) {
		boltdb.Get("overunder-"+year, league, k, func(overunder string) {

			boltdb.Get("europe-"+year, league, k, func(europe string) {

				boltdb.Get("lastmatch-"+year, league, k, func(text string) {
					b0,ff := checkAsia(asia)
					if !b0 {
						return
					}

					b4,ou:=overunderData(overunder)

					if !b4 {
						return
					}
					b1, data1 := leagueData(text)

					if !b1 {
						return
					}

					b, eu := europeData(europe)
					if !b {
						return
					}
					data := eu //[]float64{eu[0],eu[1],eu[2]}


					b2, data2 := betData(startAt, home, away, text)
					if !b2 {
						return
					}
					fn(ff, data1, data2, data,ou)
				})
			})

		})
	})
}


